package org.firecloud.h5fun.controller.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.firecloud.h5fun.model.CommonConfig;
import org.firecloud.h5fun.model.CommonFunction;
import org.firecloud.h5fun.model.CommonQuery;
import org.firecloud.h5fun.model.javabean.ApplicationBean;
import org.firecloud.h5fun.model.javabean.UserBean;

public class ShowUserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection conn = null;
		try{
			int page = 0;
			int id = Integer.parseInt(req.getParameter("id"));
			String strPage = req.getParameter("page");
			if(strPage!=null){
				page = Integer.parseInt(strPage);
			}
			CommonConfig config = (CommonConfig) req.getServletContext().getAttribute("commonConfig");
			conn = config.getConn();
			UserBean user = CommonQuery.select_user(conn, id);
			req.setAttribute("user", user);
			req.setAttribute("hiddenMail", CommonFunction.hidden_mail(user.getMail()));
			List<ApplicationBean> apps = new ArrayList<ApplicationBean>();
			int appsCount = CommonQuery.select_applications(conn, apps, user.getId(), 0,page,config.getUserInfoAppsSize());
			req.setAttribute("apps", apps);
			req.setAttribute("maxPage", (appsCount-1)/config.getUserInfoAppsSize());
			req.setAttribute("currentPage", page);
			req.getRequestDispatcher("jsp/page-show-user.jsp").forward(req, resp);
		}catch(Exception e){
			Logger.getRootLogger().error(e.toString());
			req.getRequestDispatcher("jsp/page-error.jsp").forward(req, resp);
			e.printStackTrace();
		} finally {
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {
					Logger.getRootLogger().equals(e);
				}
			}
		}
	}
	
}
